package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.pojo.TbTypeTemplateExample;
import com.dongdongshop.service.TbTypeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TbTypeTemplateServiceImpl implements TbTypeTemplateService {
    @Autowired
    private TbTypeTemplateMapper templateMapper;

    @Override
    public PageResult listTemplate(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if(name != null && !"".equals(name)){
            criteria.andNameLike("%"+ name +"%");
        }
        List<TbTypeTemplate> tbTypeTemplates = templateMapper.selectByExample(example);
        PageInfo<TbTypeTemplate> i = new PageInfo<>(tbTypeTemplates);
        return new PageResult(i.getPageNum(),i.getPageSize(),i.getTotal(),i.getPages(),i.getList());
    }

    @Override
    public void addTemplate(TbTypeTemplate tbTypeTemplate) {
        templateMapper.insertSelective(tbTypeTemplate);
    }

    @Override
    public TbTypeTemplate selectTemplate(Long id) {
        return templateMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateTemplate(TbTypeTemplate tbTypeTemplate) {
       templateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
    }

    @Override
    public void deleteTemplates(Long[] ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        templateMapper.deleteByExample(example);
    }

    @Override
    public List<TbTypeTemplate> selectTemplates() {
        return templateMapper.selectByExample(null);
    }
}
